<template>
  <div class="data-manager">
    <el-container>
      <el-header>
        <div class="header-content">
          <h2>数据管理工具</h2>
          <div class="header-actions">
            <el-button type="primary" size="small" @click="loadData">
              重新加载数据
            </el-button>
            <el-button type="danger" size="small" @click="resetData">
              重置初始数据
            </el-button>
            <el-button type="default" size="small" @click="goBack">
              返回
            </el-button>
          </div>
        </div>
      </el-header>
      
      <el-main>
        <el-tabs v-model="activeTab">
          <el-tab-pane label="用户数据" name="users">
            <el-card>
              <template #header>
                <div class="card-header">
                  <span>用户列表</span>
                  <span>总计: {{ users.length }} 个用户</span>
                </div>
              </template>
              <el-table :data="users" style="width: 100%">
                <el-table-column prop="id" label="ID" width="60" />
                <el-table-column prop="username" label="用户名" width="120" />
                <el-table-column prop="name" label="姓名" width="120" />
                <el-table-column prop="role" label="角色" width="100">
                  <template #default="{ row }">
                    <el-tag :type="row.role === 'teacher' ? 'primary' : 'success'">
                      {{ row.role === 'teacher' ? '教师' : '学生' }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column prop="email" label="邮箱" />
                <el-table-column prop="enrolledCourses" label="已选课程数" width="120">
                  <template #default="{ row }">
                    {{ row.enrolledCourses ? row.enrolledCourses.length : 0 }}
                  </template>
                </el-table-column>
              </el-table>
            </el-card>
          </el-tab-pane>
          
          <el-tab-pane label="课程数据" name="courses">
            <el-card>
              <template #header>
                <div class="card-header">
                  <span>课程列表</span>
                  <span>总计: {{ courses.length }} 门课程</span>
                </div>
              </template>
              <el-table :data="courses" style="width: 100%">
                <el-table-column prop="id" label="ID" width="60" />
                <el-table-column prop="title" label="课程名称" width="180" />
                <el-table-column prop="description" label="课程描述" />
                <el-table-column prop="teacher" label="授课教师" width="120" />
                <el-table-column label="容量/已选" width="100">
                  <template #default="{ row }">
                    <span>{{ row.enrolled }}/{{ row.capacity }}</span>
                  </template>
                </el-table-column>
                <el-table-column prop="schedule" label="上课时间" width="150" />
                <el-table-column prop="location" label="上课地点" width="120" />
                <el-table-column label="状态" width="100">
                  <template #default="{ row }">
                    <el-tag :type="row.status === 'open' ? 'success' : 'warning'">
                      {{ row.status === 'open' ? '开放' : '已满' }}
                    </el-tag>
                  </template>
                </el-table-column>
              </el-table>
            </el-card>
          </el-tab-pane>
          
          <el-tab-pane label="数据统计" name="statistics">
            <el-row :gutter="20">
              <el-col :span="8">
                <el-card>
                  <template #header>
                    <span>用户统计</span>
                  </template>
                  <div class="stat-item">
                    <div class="stat-label">总用户数</div>
                    <div class="stat-value">{{ users.length }}</div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-label">教师数</div>
                    <div class="stat-value">{{ teacherCount }}</div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-label">学生数</div>
                    <div class="stat-value">{{ studentCount }}</div>
                  </div>
                </el-card>
              </el-col>
              
              <el-col :span="8">
                <el-card>
                  <template #header>
                    <span>课程统计</span>
                  </template>
                  <div class="stat-item">
                    <div class="stat-label">总课程数</div>
                    <div class="stat-value">{{ courses.length }}</div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-label">开放课程</div>
                    <div class="stat-value">{{ openCoursesCount }}</div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-label">已满课程</div>
                    <div class="stat-value">{{ fullCoursesCount }}</div>
                  </div>
                </el-card>
              </el-col>
              
              <el-col :span="8">
                <el-card>
                  <template #header>
                    <span>选课统计</span>
                  </template>
                  <div class="stat-item">
                    <div class="stat-label">总选课数</div>
                    <div class="stat-value">{{ totalEnrollments }}</div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-label">平均选课</div>
                    <div class="stat-value">{{ avgEnrollments.toFixed(1) }}</div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-label">总容量</div>
                    <div class="stat-value">{{ totalCapacity }}</div>
                  </div>
                </el-card>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { dataManager } from '@/utils/dataManager'

const router = useRouter()
const activeTab = ref('users')

const users = ref([])
const courses = ref([])

// 统计数据
const teacherCount = computed(() => {
  return users.value.filter(u => u.role === 'teacher').length
})

const studentCount = computed(() => {
  return users.value.filter(u => u.role === 'student').length
})

const openCoursesCount = computed(() => {
  return courses.value.filter(c => c.status === 'open').length
})

const fullCoursesCount = computed(() => {
  return courses.value.filter(c => c.status === 'full').length
})

const totalEnrollments = computed(() => {
  return courses.value.reduce((sum, course) => sum + course.enrolled, 0)
})

const avgEnrollments = computed(() => {
  return courses.value.length > 0 ? totalEnrollments.value / courses.value.length : 0
})

const totalCapacity = computed(() => {
  return courses.value.reduce((sum, course) => sum + course.capacity, 0)
})

const loadData = () => {
  users.value = dataManager.getUsers()
  courses.value = dataManager.getCourses()
  ElMessage.success('数据已重新加载！')
}

const resetData = () => {
  dataManager.resetData()
  loadData()
  ElMessage.success('数据已重置为初始状态！')
}

const goBack = () => {
  router.back()
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.data-manager {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.el-header {
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 0;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
}

.header-content h2 {
  margin: 0;
  color: #303133;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 15px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
}

.stat-item:last-child {
  border-bottom: none;
}

.stat-label {
  color: #909399;
  font-size: 14px;
}

.stat-value {
  color: #303133;
  font-size: 24px;
  font-weight: bold;
}

.el-main {
  padding: 20px;
}

.el-tabs {
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

:deep(.el-tabs__content) {
  padding: 20px;
}
</style>